<template>
  <div class="chatTools_container">
    <!-- <div class="chatTools_container_top_logo mb-2">
			<img src="@/image/chatTools/index/spanceAITitle.png" class="chatTools_container_top_logo_home" alt="">
			<div class="top_logo_right pr-1">
				<img src="@/image/chatTools/index/addressBook1.png" alt="">
				<img src="@/image/chatTools/index/addIcon.png" alt="">
			</div>
		</div> -->
    <!-- <div class="chatTools_container_searchINput br-8 mb-1">
			<van-icon name="search"/>
			<span class="ml-1">{{ $t("搜索") }}</span>
		</div>
		<div class="chatTools_container_list">
			<div class="chatTools_container_list_ai">
				<img src="@/image/chatTools/index/ai_head.png" class=" mr-1 ml-1" alt="">
				<div>
					{{ $t("AI 聊天机器人") }}
				</div>
			</div>
		</div> -->

    <!-- <iframe
      src="https://imweb.bijie.world/#/pages/index/index"
      @load="onIframeLoad"
      ref="aiChatIframe"
      style="
        width: 100%;
        height: calc(100vh - 51px);
        border: none;
        overflow: hidden;
      "
    ></iframe> -->

    <!-- <iframe
      src="https://talk.testyr.top/web/#/pages/index/index"
      @load="onIframeLoad"
      ref="aiChatIframe"
      style="
        width: 100%;
        height: calc(100vh - 51px);
        border: none;
        overflow: hidden;
      "
    ></iframe> -->

    <iframe
      :src="imSrc"
      @load="onIframeLoad"
      ref="aiChatIframe"
      style="
        width: 100%;
        height: calc(100vh - 51px);
        border: none;
        overflow: hidden;
      "
    ></iframe>


    <div class="chatTools_container_loading" v-if="loading">
      <van-loading  size="24px">{{$t("加载中...")}}</van-loading>
    </div>
  </div>
</template>

<script>
export default {
  name: "ChatTools",
  data() {
    return {
      loading: true,
      imSrc: process.env.VUE_APP_IM_SRC, // im地址
      // imSrc: "https://talk.testyr.top/web/#/pages/index/index",
      // imSrc: "https://imweb.bijie.world/#/pages/index/index",
    };
  },
  mounted() {
    // 监听iframe发来的消息
    // window.addEventListener("message", this.handleIframeMessage);
    // this.$refs.aiChatIframe.onload = () => {
    //   this.$refs.aiChatIframe.contentWindow.postMessage({
    //     type: 'INIT_TOKEN',
    //     token: localStorage.getItem('imToken'),
    //     userInfo: JSON.parse(localStorage.getItem('im_userinfo'))
    //   // }, 'https://imweb.bijie.world');
    //   // }, 'https://talk.testyr.top');
    //   }, 'http://localhost:5173/#/');
      
    //   this.loading = false;
    // };

    this.$refs.aiChatIframe.onload = () => {
      this.loading = false;
    };

    window.addEventListener('message', (event) => {
      if ( this.imSrc.startsWith(event.origin) === false ) {
        return;
      }
      if ( event.data.type === 'getUserInfo') {
        event.source.postMessage({
          type: 'userInfo',
          token: localStorage.getItem('imToken'),
          userInfo: JSON.parse(localStorage.getItem('im_userinfo')),
          requestId: event.data.requestId,
          language: this.$i18n.locale
        }, event.origin);
      }
      if( event.data.type === 'IMTokenExpired') {
        this.$toast.fail(this.$t("登录已过期"));
        this.$router.push({ name: 'HomePage' });
      }
      if (event.data.type === 'IMLogout') {
        this.$toast.fail(this.$t("已退出登录"));
        this.$router.push({ name: 'HomePage' });
      }
    });
  },
  beforeDestroy() {
    // window.removeEventListener("message", this.handleIframeMessage);
  },
  methods: {
    onIframeLoad() {
      console.log("iframe loaded");
    },
		// handleIframeMessage(event) {
    //   // 验证消息来源
    //   console.log(event , 101)
    //   if (event.origin !== 'http://imweb.bijie.world') return;
    //   // 处理存储请求
    //   if (event.data.type === 'SET_STORAGE') {
    //     localStorage.setItem(event.data.key, event.data.value);
    //   }
    //   // 处理获取存储请求
    //   else if (event.data.type === 'GET_STORAGE') {
    //     const value = localStorage.getItem(event.data.key);
    //     this.$refs.aiChatIframe.contentWindow.postMessage({
    //       type: 'STORAGE_RESPONSE',
    //       key: event.data.key,
    //       value: value
    //     }, 'http://imweb.bijie.world');
    //   }
    //   // 新增错误处理
    //   else if (event.data.type === 'AXIOS_ERROR') {
    //     console.error('Iframe axios error:', event.data.error);
    //     // 可以在这里添加错误处理逻辑
    //   }
    // }
  },
};
</script>

<style>
</style>